package com.bdth.service.impl;

import com.bdth.model.Role;
import com.bdth.model.RoleFuncKey;
import com.bdth.service.IRoleFuncService;
import com.bdth.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

	@Autowired
	private IRoleFuncService roleFuncService;

	/**
	 * 添加
	 *
	 * @param role
	 * @param menuIds
	 * @throws Exception
	 */
	public void add(Role role, List<Integer> menuIds)
			throws Exception {
		super.add(role);
		addRoleMenuRel(role.getId(), menuIds);
	}


	/**
	 * 添加角色&菜单关系
	 */
	public void addRoleMenuRel(Integer roleId, List<Integer> menuIds)
			throws Exception {
		for (Integer menuid : menuIds) {
			RoleFuncKey rel = new RoleFuncKey();
			rel.setRoleID(new BigDecimal(roleId));
			rel.setFuncID(menuid);
			roleFuncService.add(rel);
		}
	}


	/**
	 * 修改
	 *
	 * @param role
	 * @param menuIds
	 * @throws Exception
	 */
	public void update(Role role, List<Integer> menuIds)
			throws Exception {
		super.update(role);
		//清除关联关系
		roleFuncService.deleteByRoleId(role.getId());
		addRoleMenuRel(role.getId(), menuIds);

	}

}
